package cn.jinjimi.app.main.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.domain.ServiceManager;
import cn.jinjimi.domain.store.ConfigStore;
import cn.jinjimi.app.entity.Area;
import cn.jinjimi.app.entity.Province;
import cn.jinjimi.app.main.adapter.ProvinceAdapter;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import java.util.concurrent.Callable;

import bolts.Continuation;
import bolts.Task;

/**
 * Created by haner on 15-1-23.
 */
public class AreaActivity extends ToolBarCommonActivity implements AdapterView.OnItemClickListener {

    private TextView tvChoose;
    private ListView mListview;
    private ProvinceAdapter mAdapter;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_area);
        setTitle(getString(R.string.info_area));
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        TextView chooseName = (TextView) findViewById(R.id.tv_choose_name);
        chooseName.setText(getString(R.string.area_had_chose));

        tvChoose = (TextView) findViewById(R.id.area_place_choosed);
        mListview = (ListView) findViewById(R.id.list_view);
        mAdapter = new ProvinceAdapter(this);
        mListview.setAdapter(mAdapter);
    }

    protected void initListener() {
        mListview.setOnItemClickListener(this);
    }

    protected void initData() {
        String province = getIntent().getStringExtra(BundleKey.PARAM_PROVINCE);
        String city = getIntent().getStringExtra(BundleKey.PARAM_CITY);

        if (StringUtils.isNotEmpty(province) && StringUtils.isNotEmpty(city)) {
            tvChoose.setText(province + " " + city);
        } else {
            tvChoose.setText(getString(R.string.info_unsetting));
        }

        showLoadingIndicator();
        getArea();
    }

    /**
     * 获取区域信息
     */
    private void getArea() {
        Task.callInBackground(new Callable<Area>() {
            @Override
            public Area call() throws Exception {
                return ServiceManager.getService(ConfigStore.class).fetchAreaList();
            }
        }).continueWith(new Continuation<Area, Void>() {
            @Override
            public Void then(Task<Area> task) throws Exception {
                Area data = task.getResult();
                if (!task.isFaulted() && data != null && data.getProvinces() != null && !data.getProvinces().isEmpty()) {
                    mAdapter.setItems(data.getProvinces());
                } else if (task.isFaulted()) {
                    ToastUtils.toastMessage(AreaActivity.this, getString(R.string.error_bad_network));
                }
                hideLoadingIndicator();
                return null;
            }
        }, Task.UI_THREAD_EXECUTOR);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Province province = mAdapter.getItem(position - mListview.getHeaderViewsCount());

        Intent intent = getIntent();
        intent.setClass(AreaActivity.this, CityActivity.class);
        intent.putExtra(BundleKey.PARAM_PROVINCE, province);
        startActivity(intent);
    }

}
